package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.passwordcontroll.passwordcontrollSubs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.passwordcontroll.CheckuserinDB;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.User;

public class PasswordDialogPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private User user;
	private JLabel lb_enterPassword, lb_enterUsername, lb_exeption;
	private JTextField tf_enterUsername;
	private JPasswordField pf_enterPasword;
	private JButton okButton;
	private JDialog parent;

	public PasswordDialogPanel(JDialog parent) {

		this.parent = parent;

		this.setPreferredSize(new Dimension(280, 200));
		this.setLayout(new GridLayout(6, 1));

		this.okButton = new JButton("Ok");
		this.okButton.addActionListener(new okButtonActionlistener());
		this.okButton.addKeyListener(new okButtonKeylistener());

		this.lb_enterPassword = new JLabel("Bitte Passwort eingeben");
		this.lb_enterUsername = new JLabel("Bitte Benutzernamen eingeben");

		this.lb_exeption = new JLabel("unsichtbar");
		this.lb_exeption.setVisible(false);

		this.tf_enterUsername = new JTextField();
		this.pf_enterPasword = new JPasswordField();
		
		this.tf_enterUsername.addKeyListener(new okButtonKeylistener());
		this.pf_enterPasword.addKeyListener(new okButtonKeylistener());

		this.add(lb_enterUsername);
		this.add(tf_enterUsername);
		this.add(lb_enterPassword);
		this.add(pf_enterPasword);
		this.add(lb_exeption);
		this.add(okButton);

	}

	public User getUser() {
		return user;
	}

	public void updateExeptionlable(String text) {
		this.lb_exeption.setText(text);
		this.lb_exeption.setVisible(true);

	}

	private void checkUser() {

		
		CheckuserinDB checkuserinDB = new CheckuserinDB();
		
		try {
	
			this.user = checkuserinDB.checkDBandGenerateUser(
					tf_enterUsername.getText(), new String(pf_enterPasword.getPassword()));
			


			if (this.user != null) {
				parent.dispose();
			} else {

				this.updateExeptionlable("Passwort nicht korrekt");
			}
		} catch (Exception e) {


			this.updateExeptionlable("Benutzer nicht gefunden");
		}

	}

	private class okButtonActionlistener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			checkUser();
		}

	}

	private class okButtonKeylistener implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {

		}

		@Override
		public void keyReleased(KeyEvent e) {

			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				checkUser();
			}

		}

		@Override
		public void keyTyped(KeyEvent e) {

		}

	}

}
